<?php

namespace App\Exceptions;

use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use \Log;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {

//        if($exception instanceof AixinException) {
//            $log = parent::renderExceptionWithWhoops($exception);
//            $logArr = explode("\n", $log);
//            Log::error($logArr[3]);
//
//        }else {
//            $log = parent::renderExceptionWithWhoops($exception);
//            $logArr = explode("\n", $log);
//            Log::error($logArr[3]);
////            Log::error($exception->getMessage());
//
//        }

        $log = parent::renderExceptionWithWhoops($exception);
        $logArr = explode("\n", $log);
        Log::error($logArr[3]);

    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if ($exception instanceof AuthenticationException) {
            if ($request->expectsJson()) {
                $result = [
                    "msg"    => 'Unauthenticated',
                    "data"=>"",
                    "status" =>  401,
                    'server_time'=>time(),
                ];
                return response()->json($result, 401);
            }

            return redirect('/login');
        }

        if ($exception instanceof RbacException) {
            if ($request->expectsJson()) {
                $result = [
                    "msg"    => $exception->getErrorMsg(),
                    "data"=>"",
                    "status" =>  $exception->getErrorNo(),
                    'server_time'=>time(),
                ];
                return response()->json($result);
            }
            return redirect('/forbidden');
        }

        if($exception instanceof AixinException) {


            $result = [
                "msg"    => $exception->getErrorMsg(),
                "data"=>"",
                "status" =>  $exception->getErrorNo(),
                'server_time'=>time(),
            ];

            return response()->json($result);

        }else {

            $result = [
                "msg"    => $exception->getMessage(),
                "data"=>"",
                "status" =>  AixinExceptionCode::COMMON_FAILD,//默认错误
                'server_time'=>time(),
            ];
            return response()->json($result);

        }
        return parent::render($request, $exception);
    }

}
